import { v4 as uuid } from 'uuid'

const annexName = '未命名'
const annexNumber = 'A'

export const contentPrototypes = [
  { id: uuid(), labelEn: 'Scope', label: '范围', dataIndex: 1, key: 21, dataType: 'scope' },
  { id: uuid(), labelEn: 'Design Basis', label: '依据性文件', dataIndex: 2, key: 22, dataType: 'design_basis' },
  {
    id: uuid(),
    labelEn: 'Normative References',
    label: '引用文件',
    dataIndex: 3,
    key: 23,
    dataType: 'normative_references'
  },
  {
    id: uuid(),
    labelEn: 'Terms and Definitions',
    label: '术语和定义',
    dataIndex: 4,
    key: 24,
    dataType: 'terms_definitions'
  },
  {
    id: uuid(),
    labelEn: 'Abbreviation',
    label: '缩略语',
    dataIndex: 5,
    key: 25,
    dataType: 'symbols_abbreviation'
  },
  { label: '未命名章节', labelEn: 'unknown name', dataIndex: 6, key: 26, dataType: 'chapter' },
  { label: '未命名章节', labelEn: 'unknown name', dataIndex: 6, key: 27, dataType: 'chapter' },
  // { label: '变量', labelEn: 'variable', dataIndex: 6, key: 28, dataType: 'variable_string' }
]
export const dataTypeLabelMap = {
  scope: '范围',
  design_basis: '依据性文件',
  normative_references: '引用文件',
  terms_definitions: '术语和定义',
  symbols_abbreviation: '缩略语'
}

export const contentLabelMap = contentPrototypes.reduce((acc, cur) => ((acc[cur.dataType] = cur.label), acc), {})
export const contentLabelEnMap = contentPrototypes.reduce((acc, cur) => ((acc[cur.dataType] = cur.labelEn), acc), {})

export const pagePrototypes = [
  // before content
  // { id: uuid(), labelEn: 'Cover', label: '封面', key: 1, pageType: 'cover' },
  // { id: uuid(), labelEn: 'Signature', label: '签署页', key: 2, pageType: 'signature' },
  // { id: uuid(), labelEn: 'Revision', label: '更改记录页', key: 3, pageType: 'revision' },
  { id: uuid(), labelEn: 'Contents', label: '目次', key: 4, pageType: 'contents' },
  { id: uuid(), labelEn: 'Foreword', label: '前言', key: 5, pageType: 'foreword' },
  { id: uuid(), labelEn: 'Introduction', label: '引言', key: 6, pageType: 'introduction' },
  // content
  { label: '正文', key: 20, children: contentPrototypes, pageType: 'content' },
  // after content
  { label: '规范性附录', key: 41, pageType: 'normativeAnnex', annexNumber, annexName },
  { label: '资料性附录', key: 42, pageType: 'informativeAnnex', annexNumber, annexName },
  { id: uuid(), labelEn: 'Bibliography', label: '参考文献', key: 51, pageType: 'reference' },
  { id: uuid(), labelEn: 'Index', label: '中文索引', key: 52, pageType: 'index' },
  { id: uuid(), labelEn: 'English Index', label: '英文索引', key: 53, pageType: 'indexEn' }
]

export const pageTypes = pagePrototypes.map((i) => i.pageType)

// key(0, 20] ：'cover', 'signature', 'revision', 'contents', 'foreword', 'introduction'
export const headTypes = pagePrototypes.filter((i) => i.key < 20).map((i) => i.pageType)

// key[21, 40]：'scope', 'design_basis', 'normative_references', 'terms_definitions', 'symbols_abbreviation', 'chapter'
export const contentTypes = contentPrototypes.map((i) => i.dataType)
export const allContentTypes = [...contentTypes, 'blank_page']
export const groupBlockTypes = ['block', 'terms'] // 'department' 行内元素单独处理，'collect' 收藏内容可以修改 'abbreviation' 只有一行

// key(40, +inf)：'normativeAnnex', 'informativeAnnex','reference', 'index'
export const tailTypes = pagePrototypes.filter((i) => i.key > 40).map((i) => i.pageType)

// 页面类型和key的映射关系，如：{ cover: 1, signature: 2, ...}
export const keyTypeMap = [...pagePrototypes, ...contentPrototypes].reduce((prev, cur) => {
  prev[cur.pageType || cur.dataType] = cur.key
  return prev
}, {})
export const typeKeyMap = [...pagePrototypes, ...contentPrototypes].reduce((prev, cur) => {
  prev[cur.key] = cur.pageType || cur.dataType
  return prev
}, {})

export const flatPrototypes = pagePrototypes.reduce((pre, cur) => {
  if (!cur?.children?.length) {
    pre.push(cur)
  } else {
    pre = [...pre, ...cur.children]
  }
  return pre
}, [])

const headKeys = pagePrototypes.filter((i) => i.key < 20).map((i) => i.key)
const tailKeys = pagePrototypes.filter((i) => i.key > 40).map((i) => i.key)
const contentKeys = contentPrototypes.map((i) => i.key)
export const isHead = (key) => headKeys.includes(key)
export const isTail = (key) => tailKeys.includes(key)
export const isContent = (key) => contentKeys.includes(key)
export const isAnnex = (key) => key === keyTypeMap['normativeAnnex'] || key === keyTypeMap['informativeAnnex']
export const isAnnexByType = (type) => type === 'normativeAnnex' || type === 'informativeAnnex' || type === 'annex'
export const isMultiType = (key) => isAnnex(key) || key === 26 || key === 27 // 26竖版27横板
export const isTailNotLast = (key) => isAnnex(key) || key === keyTypeMap['reference']
export const isListTitle = (dataType) => contentTypes.includes(dataType) || /item[1-4]/g.test(dataType)

export const spanTypes = [
  'text',
  'sub',
  'sup',
  'footnote',
  'department',
  'refdoc',
  'refchapter',
  'keyword',
  'bizIndex',
  'inline_figure',
  'inline_formula'
]

export const defaultDataTypes = [
  'scope',
  'design_basis',
  'normative_references',
  'terms_definitions',
  'symbols_abbreviation'
]
